package com.ruoyi.socket.compont;

import javax.annotation.Resource;
import javax.websocket.Session;


import com.ruoyi.socket.domain.ChatRecords;
import lombok.extern.slf4j.Slf4j;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;


import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.ArrayList;

import java.util.Date;
import java.util.List;
import java.util.Map;


@ServerEndpoint(value = "/websocket/{username}")
@Component
@Slf4j
public class WebSocketService {

//    //静态变量，用来记录当前在线连接数。应该把它设计成线程安全的。
//    private static int onlineCount = 0;

    //记录用户链接信息
    private static List<Session> userList=new ArrayList();

//    //与某个客户端的连接会话，需要通过它来给客户端发送数据
//    private Session session;
//
//    //接收username
//    private String username = "";





    /**
     * 连接建立成功调用的方法
     */
    @OnOpen
    public void onOpen(Session session,@PathParam("username")String username) {
//        this.session = session;
//        this.username = username;
        userList.add(session);

        //用来存放用户信息
        Map<String, Object> userProperties = session.getUserProperties();
        userProperties.put("username", username);
         log.info("--"+username+"链接");

            log.info("有新窗口开始监听:" + username + ",当前在线人数为:" + userList.size());


    }

    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose(Session session) {
         userList.remove(session); //在线数减1
        //断开连接情况下，更新主板占用情况为释放
        //这里写你 释放的时候，要处理的业务
        log.info("有一连接关闭！当前在线人数为" + userList.size());

    }

    /**
     * 收到客户端消息后调用的方法
     *
     * @param message 客户端发送过来的消息
     */
    @Resource
    private MongoTemplate mongoTemplate;

    @OnMessage
    public void onMessage(String message, Session session) throws IOException {
        Map<String, Object> userProperties = session.getUserProperties();
        String username = (String) userProperties.get("username");

        //MongoDB 记录聊天
        ChatRecords chatRecords = new ChatRecords();
        chatRecords.setContent(message);
        chatRecords.setUsername(username);
        chatRecords.setCreateTime(new Date());
        mongoTemplate.save(chatRecords);

        for (Session user : userList) {
            user.getBasicRemote().sendText(username+":"+message);
        }

        System.out.println("当前用户:"+username+",发送信息:"+message);

    }

    /**
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error) {
        log.error(session.getId());
        error.printStackTrace();
    }







//    public static synchronized int getOnlineCount() {
//        return onlineCount;
//    }
//
//    public static synchronized void addOnlineCount() {
//        WebSocketService.onlineCount++;
//    }
//
//    public static synchronized void subOnlineCount() {
//        WebSocketService.onlineCount--;
//    }





}
